package com.surelution.projects.crm
import org.grails.plugins.excelimport.ExcelImportUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import java.text.SimpleDateFormat
import java.util.Map;

class NoGasPlanController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    def index = {
        redirect(action: "list", params: params)
    }
	static Map CONFIG_NOGAS_PLAN_MAP = [
		sheet:"a",
		startRow: 0,
		columnMap:  [
			'A':'station',
			'B':'stationCode',
			'C':'planNumber',
		]
	]
	
	def ImportExcelService
	def springSecurityService
	def planImport = {
		def file = params.planFile
		def fileName = file.originalFilename
		def time
		Date planTime
		Workbook workbook = WorkbookFactory.create(file.inputStream)
		List noGasList = ExcelImportUtils.columns(workbook, CONFIG_NOGAS_PLAN_MAP)
		noGasList.each{
			if (it.get("stationCode")){
				if (it.get("station") == "站名"){
					time = it.get("planNumber")
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM")
					planTime = sdf.parse(time)
				}
				else{
					OwnStation station = OwnStation.findByCode(it.get("stationCode"))
					if (!NoGasPlanSum.findByStationAndPlanTime(station,planTime)){
						NoGasPlanSum noGasPlanSum = new NoGasPlanSum(station:station,planTime:planTime)
						noGasPlanSum.station = station
						float planNumber = Float.parseFloat("${it.get("planNumber")}")
						noGasPlanSum.planNums = planNumber
						noGasPlanSum.save()
						flash.message = "${message(code: 'default.updateFileSuccess.message', args: [message(code: 'noGasPlan.label', default: 'GasList')])}"
					}
					else{
						def noGasPlanSum = NoGasPlanSum.findByStationAndPlanTime(station,planTime)
						float planNumber = Float.parseFloat("${it.get("planNumber")}")
						noGasPlanSum.planNums = planNumber
						noGasPlanSum.save()
						flash.message = "${message(code: 'default.updateFileSuccess.message', args: [message(code: 'noGasPlan.label', default: 'GasList')])}"
					}
				}
			}
		}
		ImportExcel excel = ImportExcelService.storeFile(file.inputStream,fileName)
		excel.uploader = springSecurityService.principal.username
		excel.type = "非油品销售计划"
		excel.save()
        redirect(action: "list", controller:"noGasPlanSum")
	}
	
    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [noGasPlanInstanceList: NoGasPlan.list(params), noGasPlanInstanceTotal: NoGasPlan.count()]
    }

    def create = {
        def noGasPlanInstance = new NoGasPlan()
        noGasPlanInstance.properties = params
        return [noGasPlanInstance: noGasPlanInstance]
    }

    def save = {
        def noGasPlanInstance = new NoGasPlan(params)
        if (noGasPlanInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'noGasPlan.label', default: 'NoGasPlan'), noGasPlanInstance.id])}"
            redirect(action: "show", id: noGasPlanInstance.id)
        }
        else {
            render(view: "create", model: [noGasPlanInstance: noGasPlanInstance])
        }
    }

    def show = {
        def noGasPlanInstance = NoGasPlan.get(params.id)
        if (!noGasPlanInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'noGasPlan.label', default: 'NoGasPlan'), params.id])}"
            redirect(action: "list")
        }
        else {
            [noGasPlanInstance: noGasPlanInstance]
        }
    }

    def edit = {
        def noGasPlanInstance = NoGasPlan.get(params.id)
        if (!noGasPlanInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'noGasPlan.label', default: 'NoGasPlan'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [noGasPlanInstance: noGasPlanInstance]
        }
    }

    def update = {
        def noGasPlanInstance = NoGasPlan.get(params.id)
        if (noGasPlanInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (noGasPlanInstance.version > version) {
                    
                    noGasPlanInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'noGasPlan.label', default: 'NoGasPlan')] as Object[], "Another user has updated this NoGasPlan while you were editing")
                    render(view: "edit", model: [noGasPlanInstance: noGasPlanInstance])
                    return
                }
            }
            noGasPlanInstance.properties = params
            if (!noGasPlanInstance.hasErrors() && noGasPlanInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'noGasPlan.label', default: 'NoGasPlan'), noGasPlanInstance.id])}"
                redirect(action: "show", id: noGasPlanInstance.id)
            }
            else {
                render(view: "edit", model: [noGasPlanInstance: noGasPlanInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'noGasPlan.label', default: 'NoGasPlan'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def noGasPlanInstance = NoGasPlan.get(params.id)
        if (noGasPlanInstance) {
            try {
                noGasPlanInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'noGasPlan.label', default: 'NoGasPlan'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'noGasPlan.label', default: 'NoGasPlan'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'noGasPlan.label', default: 'NoGasPlan'), params.id])}"
            redirect(action: "list")
        }
    }
}
